<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>验证消息管理</title>
    <link rel="stylesheet" href="../assets/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../assets/style/admin.css">
</head>
<body class="layui-layout-body">
<div class="layui-layout layui-layout-admin">
    <script src="../assets/layui/layui.js"></script>
    <script src="../assets/js/common.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
    <script src="../common/header.js"></script>
    <!-- 内容主体区域 -->
    <div id="LAY_app_body">
        <div class="layui-fluid">
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md12">
                    <div class="layui-card">
                        <div class="layui-card-header">
                            验证消息管理
                        </div>
                        <div class="layui-card-body">
                            <div class="layui-row">
                                <select id="status" style="position: relative;float: right;" onchange="reloadList()">
                                    <option value="1">启用的</option>
                                    <option value="0">禁用的</option>
                                    <option value="-1">全部</option>
                                </select>&nbsp;&nbsp;
                                <a href="editGroupEnhance.html" class="layui-btn layui-btn-sm layui-btn-radius"><i class="layui-icon layui-icon-add-1"></i>新增验证消息组</a>
                            </div>
                            <table id="LAY-message-group-manage" lay-filter="LAY-message-group-manage"></table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
        var reloadList;
        layui.use('table', function(){
            var $ = layui.$
                ,table = layui.table
                ,form = layui.form;

            var status = $('#status').find("option:selected").val();
            table.render({
                elem: '#LAY-message-group-manage'
//            ,height: 315
                ,url: '/message/groupListData' //数据接口
                ,where: {//设定异步数据接口的额外参数
                    status: status
                }
                ,page: true //开启分页
                ,cols: [[ //表头
//                {field: 'id', title: 'ID', width:80, sort: true, fixed: 'left'}
                    {field: 'name', title: '验证消息组名称', fixed: 'left'}
                    ,{field: 'message_num', title: '验证消息条数', width:150}
                    ,{field: 'created', title: '添加时间', width:170, sort: true}
                    ,{field: 'status', title: '状态', sort: true, width:100, templet: '#statusTpl', unresize: true}
                    ,{fixed: 'right', width:150, align:'center', toolbar: '#barDemo'}
                ]]
                ,title: '验证消息组列表'
//            ,toolbar: '#toolbarDemo'
//            ,defaultToolbar: ['filter']
            });

            //监听工具条
            table.on('tool(LAY-message-group-manage)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
                var data = obj.data; //获得当前行数据
                var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
                var tr = obj.tr; //获得当前行 tr 的DOM对象

                if(layEvent === 'detail'){ //查看
                    //do somehing
                } else if(layEvent === 'del'){ //删除

                } else if(layEvent === 'edit'){ //编辑
                    //do something
                }
            });

            //监听状态操作
            form.on('switch(statusDemo)', function(obj){
                var status = 0;
                if (obj.elem.checked) {
                    status = 1;
                }
                var id = this.step;
                var post = {
                    id: id,
                    status: status
                };
                $.ajax({
                    type: "POST",
                    url: "/message/changeGroupStatus",
                    data: post,
                    success:function(response){
                        var dataObj = $.parseJSON(response);
                        if(dataObj.code == 0) {
                            layer.tips('修改成功', obj.othis);
                        } else if(dataObj.code == -1) {
                            layer.tips(dataObj.msg, obj.othis);
                        } else {
                            layer.msg(dataObj.msg, {icon: 5});
                        }

                        setTimeout(function(){
                            reloadList();
                        }, 1000);
                    },
                    error: function (request, status, error) {

                    }
                });
                return false;
            });

            reloadList = function () {
                var status = $('#status').find("option:selected").val();
                table.reload('LAY-message-group-manage', {
                    where: { //设定异步数据接口的额外参数，任意设
                        status: status
                    }
                    ,page: {
                        curr: 1 //重新从第 1 页开始
                    }
                });
            };
        });

    </script>
    <script type="text/html" id="barDemo">
        <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="edit" href="/message/editGroupEnhance/{{d.id}}" target="_self"><i class="layui-icon layui-icon-edit"></i>编辑</a>
    </script>
    <script type="text/html" id="statusTpl">
        <input type="checkbox"  name="status" step="{{d.id}}" value="{{d.status}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="statusDemo" {{ d.status == 1 ? 'checked' : '' }}>
    </script>
</div>
</body>
</html>
